package com.example.hehecat.yuejian1.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ActivityListContract;
import com.example.hehecat.yuejian1.mvp.model.bean.ActivityBean;
import com.example.hehecat.yuejian1.mvp.presenter.ActivityListPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ActivityListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.ContentValues.TAG;


public class ActivityListFragment extends Fragment implements  ActivityListContract.View {

    Unbinder unbinder;
    @BindView(R.id.recycle_list)
    RecyclerView activityList;

    private ActivityListAdapter adapter;
    private ActivityListPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recview, container, false);
        unbinder = ButterKnife.bind(this, view);

        //创建布局管理
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        activityList.setLayoutManager(layoutManager);
        activityList.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ActivityListAdapter(R.layout.item_list, getContext());
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setNotDoAnimationCount(2);

        //监听item点击
        adapter.setOnItemClickListener((madapter, view1, position) ->
                moveToActivityInfo((adapter.getItem(position)).getActivityid()));

        //给RecyclerView设置适配器
        presenter = new ActivityListPresenter(this);
        presenter.updateAdapterData();
        activityList.setAdapter(adapter);
        return view;
    }

    public static ActivityListFragment newInstance(String name) {
        ActivityListFragment fragment = new ActivityListFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void updateAdapterData(List<ActivityBean> data) {
        adapter.addData(data);
    }

    @Override
    public void moveToActivityInfo(int activity_id) {
        ActivityInfoFragment activityInfoFragment = ActivityInfoFragment.newInstance(activity_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, activityInfoFragment, null)
                .commit();
    }


}
